import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import utils.ToMSSE as tocsv

fontStyle = \
    {'family': 'Times New Roman',
     'size': 20,

    }
def a():
    #绘制平均MSSE
    origin_file_name = "best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/MSSE.csv"
    data = pd.read_csv(origin_file_name, index_col=0)
    ob = data
    means = np.mean(ob, axis=1)
    std_devs = np.std(ob, axis=1)
    upper_bound = means + std_devs
    lower_bound = means - std_devs
    #"MSSE_141	MSSE_515	MSSE_407	MSSE_221	MSSE_350"
    timesteps = np.arange(data["MSSE_141"].shape[0])
    plt.plot(timesteps, means, label="Aver MSSE")
    plt.fill_between(timesteps, lower_bound, upper_bound, alpha=0.5)
    plt.legend()
    plt.show()
def b(origin_file_name, origin_file_asu, saved_png_path):
    # 绘制最小MSSE
    # origin_file_name:MSSE数据表，内有多次实验的MSSE数据,每组数据运行次数相等
    # origin_file_asu：ASU数据表，内有多次实验的ASU数据,每组数据运行次数相等
    # saved_png_path:所存储图片的地址
    data = pd.read_csv(origin_file_name, index_col=0)
    data_asu = pd.read_csv(origin_file_asu, index_col=0)
    #    "MSSE_141	MSSE_515	MSSE_407	MSSE_221	MSSE_350"
    msse_ = data.columns.tolist()#["MSSE_141", "MSSE_515", "MSSE_407", "MSSE_221", "MSSE_350"]
    asu_ = data_asu.columns.tolist()#["ASU_141", "ASU_515", "ASU_407", "ASU_221", "ASU_350"]
    ob = np.zeros((data[msse_[0]].shape[0], len(msse_)))

    for j in range(len(msse_)):
        min = 10000
        now_ASUs = data_asu[asu_[j]]
        max_asu = now_ASUs[1]
        now_MSSEs = data[msse_[j]]
        history_asu = 0.0
        for i in range(1, len(now_MSSEs)):
            if max_asu < now_ASUs[i]:
                history_asu = max_asu
                max_asu = now_ASUs[i]
                min = 10000
            if (min > now_MSSEs[i]) and (history_asu < now_ASUs[i]):
                min = now_MSSEs[i]
            ob[i, j] = min
    means = np.mean(ob, axis=1)
    std_devs = np.std(ob, axis=1)
    upper_bound = means + std_devs
    lower_bound = means - std_devs
    timesteps = np.arange(data[msse_[0]].shape[0])
    plt.plot(timesteps, means, label="Min MSSE")
    plt.fill_between(timesteps, lower_bound, upper_bound, alpha=0.5)
    plt.title("Minimum MSSE fluctuation range for Task8")
    plt.xlabel("Episode")
    plt.ylabel("Minimum of MSSE")
    plt.legend()
    plt.savefig(saved_png_path)
    plt.show()
    # plt.plot(timesteps, ob[:, 1], label="Min MSSE")
    # plt.show()
def b_all(task_name,origin_file_name1,origin_file_asu1,origin_file_name2,origin_file_asu2,saved_png_path,title_name, beilv,a):
    # 绘制最小MSSE
    # origin_file_name:MSSE数据表，内有多次实验的MSSE数据,每组数据运行次数相等
    # origin_file_asu：ASU数据表，内有多次实验的ASU数据,每组数据运行次数相等
    # saved_png_path:所存储图片的地址
    #title_name: 图片标题的名字
    data = pd.read_csv(origin_file_name1, index_col=0)
    data_asu = pd.read_csv(origin_file_asu1, index_col=0)
    #    "MSSE_141	MSSE_515	MSSE_407	MSSE_221	MSSE_350"
    msse_ = data.columns.tolist()  # ["MSSE_141", "MSSE_515", "MSSE_407", "MSSE_221", "MSSE_350"]
    asu_ = data_asu.columns.tolist()  # ["ASU_141", "ASU_515", "ASU_407", "ASU_221", "ASU_350"]
    plt.figure(figsize=(8, 6))
    ob = np.zeros((data[msse_[0]].shape[0], len(msse_)) )
    for j in range(len(msse_)):
        min = 10000
        now_ASUs = data_asu[asu_[j]]
        max_asu = now_ASUs[1]
        now_MSSEs = data[msse_[j]]
        history_asu = 0.0
        for i in range(1, len(now_MSSEs)):
            if max_asu < now_ASUs[i]:
                history_asu = max_asu
                max_asu = now_ASUs[i]
                min = 10000
            if (min > now_MSSEs[i]) and (history_asu < now_ASUs[i]):
                min = now_MSSEs[i]
            ob[i, j] = min
    means = np.mean(ob, axis=1)
    std_devs = np.std(ob, axis=1)
    upper_bound = means + std_devs
    lower_bound = means - std_devs
    saved_list = []
    saved_list.append(pd.DataFrame(columns=["minMSSE"], data=means))
    saved_list.append(pd.DataFrame(columns=["minMSSE_STD"], data=std_devs))

    # tocsv.to_csv(task_name + "/sigmoid_softplus_minmsse.csv", ["minMSSE", "minMSSE_STD"], saved_list)

    timesteps = np.arange(data[msse_[0]].shape[0])
    plt.plot(timesteps, means, label="sigmoid")
    plt.fill_between(timesteps, lower_bound, upper_bound, alpha=a)
    data = pd.read_csv(origin_file_name2, index_col=0)
    data_asu = pd.read_csv(origin_file_asu2, index_col=0)
    #    "MSSE_141	MSSE_515	MSSE_407	MSSE_221	MSSE_350"
    msse_ = data.columns.tolist()  # ["MSSE_141", "MSSE_515", "MSSE_407", "MSSE_221", "MSSE_350"]
    asu_ = data_asu.columns.tolist()  # ["ASU_141", "ASU_515", "ASU_407", "ASU_221", "ASU_350"]
    ob = np.zeros((data[msse_[0]].shape[0], len(msse_)))
    for j in range(len(msse_)):
        min = 10000
        now_ASUs = data_asu[asu_[j]]
        max_asu = now_ASUs[1]
        now_MSSEs = data[msse_[j]]
        history_asu = 0.0
        for i in range(1, len(now_MSSEs)):
            if max_asu < now_ASUs[i]:
                history_asu = max_asu
                max_asu = now_ASUs[i]
                min = 10000
            if (min > now_MSSEs[i]) and (history_asu < now_ASUs[i]):
                min = now_MSSEs[i]
            ob[i, j] = min
    means = np.mean(ob, axis=1)
    std_devs = np.std(ob, axis=1)
    upper_bound = means + std_devs
    lower_bound = means - std_devs
    timesteps = np.arange(data[msse_[0]].shape[0])
    saved_list.append(pd.DataFrame(columns=["minMSSE_softplus"], data=means))
    saved_list.append(pd.DataFrame(columns=["minMSSE_STD_softplus"], data=std_devs))

    tocsv.to_csv(task_name + "/sigmoid_softplus_minmsse.csv", ["minMSSE", "minMSSE_STD", "minMSSE_softplus", "minMSSE_STD_softplus"], saved_list)

    plt.plot(timesteps, means, label="softplus")
    plt.fill_between(timesteps, lower_bound, upper_bound, alpha=0.5)

    plt.title(title_name, fontdict=fontStyle)
    # plt.title("Minimum MSSE fluctuation range for Task7 and Task8 of sigmoid")
    plt.xlabel("Episode", fontdict=fontStyle)
    plt.ylabel("Minimum of MSSE", fontdict=fontStyle)
    plt.legend(loc='lower right')
    plt.savefig(saved_png_path)
    plt.show()
def e(saved_png_path):
    #绘制5组误差带图MSSE
    origin_file_name = "best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/MSSE.csv"
    data = pd.read_csv(origin_file_name, index_col=0)
    #ob = np.zeros((5, data["MSSE"].shape[0], 5))
    msse_ = data.columns.tolist()
    list_ = []
    for i in range(5):
        data_1 = data[msse_[i]]
        reshaped_series = data_1[:-1].values.reshape(-1, 5)
        means = np.mean(reshaped_series, axis=1)
        std_devs = np.std(reshaped_series, axis=1)
        upper_bound = means + std_devs
        lower_bound = means - std_devs
        timesteps = np.arange(reshaped_series.shape[0])
        plt.plot(timesteps, means)
        plt.fill_between(timesteps, lower_bound, upper_bound, alpha=0.5)
        plt.legend()
    plt.title("Sigmoid as the activation function to solve the  Task 8.")
    plt.xlabel("Episode")
    plt.ylabel("MSSE")
    plt.savefig(saved_png_path)
    plt.show()
def f():
    #绘制5组MSSE
    origin_file_name = "best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/MSSE.csv"
    data = pd.read_csv(origin_file_name, index_col=0)
    #ob = np.zeros((5, data["MSSE"].shape[0], 5))
    msse_ = data.columns.tolist()
    data_1 = data[msse_[0]]
    timesteps = np.arange(data_1.shape[0])
    plt.plot(timesteps, data_1, label="Aver MSSE")
    plt.legend()
    plt.show()
def g():
    # 绘制5组误差带图ASU
    origin_file_name = "best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/ASU.csv"
    data = pd.read_csv(origin_file_name, index_col=0)
    # ob = np.zeros((5, data["MSSE"].shape[0], 5))
    msse_ = ["ASU", "ASU.1", "ASU.2", "ASU.3", "ASU.4"]
    list_ = []
    for i in range(5):
        data_1 = data[msse_[i]]
        reshaped_series = data_1[:-1].values.reshape(-1, 5)
        means = np.mean(reshaped_series, axis=1)
        std_devs = np.std(reshaped_series, axis=1)
        upper_bound = means + std_devs
        lower_bound = means - std_devs
        timesteps = np.arange(reshaped_series.shape[0])
        plt.plot(timesteps, means, label="Aver ASU")
        plt.fill_between(timesteps, lower_bound, upper_bound, alpha=0.5)
        plt.legend()
    plt.show()
def h():
    # 绘制5组误差带图Rewards
    origin_file_name = "best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/Reward.csv"
    data = pd.read_csv(origin_file_name, index_col=0)
    # ob = np.zeros((5, data["MSSE"].shape[0], 5))
    msse_ = ["Rewards", "Rewards.1", "Rewards.2", "Rewards.3", "Rewards.4"]
    list_ = []
    for i in range(5):
        data_1 = data[msse_[i]]
        reshaped_series = data_1[:-1].values.reshape(-1, 5)
        means = np.mean(reshaped_series, axis=1)
        std_devs = np.std(reshaped_series, axis=1)
        upper_bound = means + std_devs
        lower_bound = means - std_devs
        timesteps = np.arange(reshaped_series.shape[0])
        plt.plot(timesteps, means, label="Aver Rewards")
        plt.fill_between(timesteps, lower_bound, upper_bound, alpha=0.5)
        plt.legend()
    plt.show()
def i():
    # 绘制5组误差带图Rewards
    origin_file_name = "best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/TopSIS.csv"
    data = pd.read_csv(origin_file_name, index_col=0)
    # ob = np.zeros((5, data["MSSE"].shape[0], 5))
    msse_ = ["Topsis", "Topsis.1", "Topsis.2", "Topsis.3", "Topsis.4"]
    for i in range(5):
        data_1 = data[msse_[i]]
        reshaped_series = data_1.values.reshape(-1, 5)
        means = np.mean(reshaped_series, axis=1)
        std_devs = np.std(reshaped_series, axis=1)
        upper_bound = means + std_devs
        lower_bound = means - std_devs
        timesteps = np.arange(reshaped_series.shape[0])
        plt.plot(timesteps, means, label="Aver Topsis")
        plt.fill_between(timesteps, lower_bound, upper_bound, alpha=0.5)
        plt.legend()
    plt.show()
def j():
    # 绘制5组误差带图Rewards
    origin_file_name = "best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/LOSS.csv"
    data = pd.read_csv(origin_file_name, index_col=0)
    # ob = np.zeros((5, data["MSSE"].shape[0], 5))
    msse_ = ["Topsis", "Topsis.1", "Topsis.2", "Topsis.3", "Topsis.4"]
    for i in range(5):
        data_1 = data[msse_[i]]
        reshaped_series = data_1.values.reshape(-1, 5)
        means = np.mean(reshaped_series, axis=1)
        std_devs = np.std(reshaped_series, axis=1)
        upper_bound = means + std_devs
        lower_bound = means - std_devs
        timesteps = np.arange(reshaped_series.shape[0])
        plt.plot(timesteps, means, label="Aver Topsis")
        plt.fill_between(timesteps, lower_bound, upper_bound, alpha=0.5)
        plt.legend()
    plt.show()
def pltMinMSSETask8_sigmoid_softplus(beilv,a,task_name):
    origin_file_name_Task8 = "best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/MSSE.csv"
    origin_file_asu_Task8 = "best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/ASU.csv"
    # saved_png_path = "best_orders/Trans_sigmoid_MSSE_ASU_Reward/img/minmsse.png"
    origin_file_name_Task7 = "best_orders/Task8/Trans_softplus_MSSE_ASU_Reward/MSSE.csv"
    origin_file_asu_Task7 = "best_orders/Task8/Trans_softplus_MSSE_ASU_Reward/ASU.csv"
    # saved_png_path = "best_orders/Trans_softplus_MSSE_ASU_Reward/img/minmsse.png"
    saved_png_path = "best_orders/Task7_And_Task8/img/minmsse.png"
    # b(origin_file_name, origin_file_asu, saved_png_path)
    tn = "Minimum MSSE for Task8 of sigmoid and softplus"
    b_all(task_name, origin_file_name_Task8, origin_file_asu_Task8, origin_file_name_Task7, origin_file_asu_Task7, saved_png_path,tn,beilv,a)
def pltMinMSSETask7_sigmoid_softplus(beilv,a,task_name):
    origin_file_name_Task8 = "best_orders/Task7/Trans_sigmoid_MSSE_ASU_Reward/MSSE.csv"
    origin_file_asu_Task8 = "best_orders/Task7/Trans_sigmoid_MSSE_ASU_Reward/ASU.csv"
    # saved_png_path = "best_orders/Trans_sigmoid_MSSE_ASU_Reward/img/minmsse.png"
    origin_file_name_Task7 = "best_orders/Task7/Trans_softplus_MSSE_ASU_Reward/MSSE.csv"
    origin_file_asu_Task7 = "best_orders/Task7/Trans_softplus_MSSE_ASU_Reward/ASU.csv"
    # saved_png_path = "best_orders/Trans_softplus_MSSE_ASU_Reward/img/minmsse.png"
    saved_png_path = "best_orders/Task7_And_Task8/img/minmsse_Task7.png"
    # b(origin_file_name, origin_file_asu, saved_png_path)
    tn = "Minimum MSSE for Task7 of sigmoid and softplus"
    b_all(task_name,origin_file_name_Task8, origin_file_asu_Task8, origin_file_name_Task7, origin_file_asu_Task7, saved_png_path, tn, beilv,a)
def pltMSSEBar_Abstract(origin_file_name, saved_png_path, Task_Num,activate_fun,beilv,alpha):
    #绘制5组误差带图MSSE
    #主路径名称：saved_png_path
    # origin_file_name = "best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/MSSE.csv"
    data = pd.read_csv(origin_file_name, index_col=0)
    #ob = np.zeros((5, data["MSSE"].shape[0], 5))
    msse_ = data.columns.tolist()
    plt.figure(figsize=(8, 6))
    label_ = ["First time", "Second time", "Third time", "Fourth time", "Fifth time", ]
    for i in range(len(msse_)):
        data_1 = data[msse_[i]]
        reshaped_series = data_1[:-1].values.reshape(-1, len(msse_) * beilv)
        means = np.mean(reshaped_series, axis=1)
        std_devs = np.std(reshaped_series, axis=1)
        upper_bound = means + std_devs
        lower_bound = means - std_devs
        timesteps = np.arange(reshaped_series.shape[0])
        plt.plot(timesteps, means, label=label_[i])
        plt.fill_between(timesteps, lower_bound, upper_bound, alpha=alpha)
        plt.legend(loc='lower right')

        # tocsv(saved_png_path + csv_name, "MSSE")
    plt.title(activate_fun + " as the activation function to solve the " + Task_Num, fontdict=fontStyle) #Task 8.
    plt.xlabel("Episode", fontdict=fontStyle)
    plt.ylabel("MSSE", fontdict=fontStyle)
    plt.savefig(saved_png_path)
    plt.show()
def pltASUBar_Abstract(origin_file_name, saved_png_path, Task_Num,activate_fun,beilv,alpha):
    #绘制5组误差带图MSSE
    # origin_file_name = "best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/MSSE.csv"
    data = pd.read_csv(origin_file_name, index_col=0)
    #ob = np.zeros((5, data["MSSE"].shape[0], 5))
    msse_ = data.columns.tolist()
    plt.figure(figsize=(8, 6))
    label_ = ["First time", "Second time", "Third time", "Fourth time", "Fifth time", ]
    for i in range(len(msse_)):
        data_1 = data[msse_[i]]
        reshaped_series = data_1[:-1].values.reshape(-1, len(msse_) * beilv)
        means = np.mean(reshaped_series, axis=1)
        std_devs = np.std(reshaped_series, axis=1)
        upper_bound = means + std_devs
        lower_bound = means - std_devs
        timesteps = np.arange(reshaped_series.shape[0])
        plt.plot(timesteps, means, label=label_[i])
        plt.fill_between(timesteps, lower_bound, upper_bound, alpha=alpha)
        plt.legend(loc='lower right')
    plt.title(activate_fun + " as the activation function to solve the " + Task_Num, fontdict=fontStyle) #Task 8.
    plt.xlabel("Episode", fontdict=fontStyle)
    plt.ylabel("ASU", fontdict=fontStyle)
    plt.savefig(saved_png_path)
    plt.show()
def pltTopsisBar_Abstract(origin_file_name, saved_png_path, Task_Num,activate_fun,beilv,alpha):
    #绘制5组误差带图MSSE
    # origin_file_name = "best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/MSSE.csv"
    data = pd.read_csv(origin_file_name, index_col=0)
    #ob = np.zeros((5, data["MSSE"].shape[0], 5))
    msse_ = data.columns.tolist()
    plt.figure(figsize=(8, 6))
    label_ = ["First time", "Second time", "Third time", "Fourth time", "Fifth time", ]
    for i in range(len(msse_)):
        data_1 = data[msse_[i]]
        reshaped_series = data_1[:-1].values.reshape(-1, len(msse_) * beilv)
        means = np.mean(reshaped_series, axis=1)
        std_devs = np.std(reshaped_series, axis=1)
        upper_bound = means + std_devs
        lower_bound = means - std_devs
        timesteps = np.arange(reshaped_series.shape[0])
        plt.plot(timesteps, means, label=label_[i])
        plt.fill_between(timesteps, lower_bound, upper_bound, alpha=alpha)
        plt.legend(loc='lower right')
    plt.title(activate_fun + " as the activation function to solve the " + Task_Num, fontdict=fontStyle) #Task 8.
    plt.xlabel("Episode", fontdict=fontStyle)
    plt.ylabel("Topsis", fontdict=fontStyle)
    plt.savefig(saved_png_path)
    plt.show()
def pltRewardBar_Abstract(origin_file_name, saved_png_path, Task_Num,activate_fun,beilv, alpha):
    #绘制5组误差带图MSSE
    # origin_file_name = "best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/MSSE.csv"
    data = pd.read_csv(origin_file_name, index_col=0)
    #ob = np.zeros((5, data["MSSE"].shape[0], 5))
    msse_ = data.columns.tolist()
    plt.figure(figsize=(8, 6))
    label_ = ["First time", "Second time", "Third time", "Fourth time", "Fifth time", ]
    for i in range(len(msse_)):
        data_1 = data[msse_[i]]
        reshaped_series = data_1[:-1].values.reshape(-1, len(msse_) * beilv)
        means = np.mean(reshaped_series, axis=1)
        std_devs = np.std(reshaped_series, axis=1)
        upper_bound = means + std_devs
        lower_bound = means - std_devs
        timesteps = np.arange(reshaped_series.shape[0])
        plt.plot(timesteps, means, label=label_[i])
        plt.fill_between(timesteps, lower_bound, upper_bound, alpha=alpha)
        plt.legend(loc='lower right')
    plt.title(activate_fun +" as the activation function to solve the " + Task_Num, fontdict=fontStyle) #Task 8.
    plt.xlabel("Episode", fontdict=fontStyle)
    plt.ylabel("Rewards", fontdict=fontStyle)
    plt.savefig(saved_png_path)
    plt.show()
def getASU_MSSE(asu_file_name,msse_file_name,saved_path):
    #绘制ASU与MSSE的交叉图像
    #第一步：处理5次实验的结果，求均值
#ASU
    #asu_file_name = "best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/ASU.csv"
    asu = pd.read_csv(asu_file_name)
    asu_list_name = asu.columns.tolist()
    asu_means = asu[asu_list_name[1:]].mean(axis=1)
    asu_std_devs = asu[asu_list_name[1:]].std(axis=1)
    asu_mean_list = []
    asu_std_devs_list = []
    for i in asu_means[:-1]:
        asu_mean_list.append(i)
    for i in asu_std_devs[:-1]:
        asu_std_devs_list.append(i)
    # upper_bound = asu_means + asu_std_devs
    # lower_bound = asu_means - asu_std_devs
    # timesteps = np.arange(asu.shape[0])
    # ax.plot(timesteps, asu_means)  # add x-axis label
    # ax.set_xlabel('Year', fontsize=14)  # add y-axis label
    # ax.set_ylabel('Sales', fontsize=16)  # define second y-axis that shares x-axis with current plot
    # ax.fill_between(timesteps, lower_bound, upper_bound, alpha=alpha)
    # # plt.legend(loc='lower right')
#MSSE
    #msse_file_name = "best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/MSSE.csv"
    msse_means, msse_std_devs = get_min_msse(msse_file_name, asu_file_name)
    msse_means_list = []
    for i in msse_means[:-1]:
        msse_means_list.append(i)
    msse_std_devs_list = []
    for i in msse_std_devs[:-1]:
        msse_std_devs_list.append(i)
    list_ = []
    list_.append(asu_mean_list)
    list_.append(asu_std_devs_list)
    list_.append(msse_means_list)
    list_.append(msse_std_devs_list)

    tocsv.list_to_csv(saved_path, ["asu_mean", "asu_std", "msse_mean", "msse_std"], list_)#df = df.reset_index(drop=True)
#     upper_bound = msse_means + msse_std_devs
#     lower_bound = msse_means - msse_std_devs
#     timesteps = np.arange(msse_means.shape[0])
#     ax2 = ax.twinx()  # add second line to plot
#     ax2.plot(timesteps, msse_means, color="red")  # add second y-axis label
#     ax2.set_ylabel('Leads', fontsize=16)
#     ax2.fill_between(timesteps, lower_bound, upper_bound, alpha=alpha)
#     # ax2.legend(loc='lower right')
# #combine
#     plt.title("ASU_MSSE", fontdict=fontStyle) #Task 8.
#     plt.xlabel("Episode", fontdict=fontStyle)
#     plt.ylabel("MSSE", fontdict=fontStyle)
#     # plt.savefig(saved_png_path)
#     plt.show()

def get_min_msse(origin_file_name1, origin_file_asu1):
    # 绘制最小MSSE origin_file_name1:MSSE
    data = pd.read_csv(origin_file_name1, index_col=0)
    data_asu = pd.read_csv(origin_file_asu1, index_col=0)
    msse_ = data.columns.tolist()  # ["MSSE_141", "MSSE_515", "MSSE_407", "MSSE_221", "MSSE_350"]
    asu_ = data_asu.columns.tolist()  # ["ASU_141", "ASU_515", "ASU_407", "ASU_221", "ASU_350"]
    ob = np.zeros((data[msse_[0]].shape[0], len(msse_)))
    for j in range(len(msse_)):
        min = 10000
        now_ASUs = data_asu[asu_[j]]
        max_asu = now_ASUs[0]
        now_MSSEs = data[msse_[j]]
        history_asu = 0.0
        for i in range(1, len(now_MSSEs)):
            if max_asu < now_ASUs[i]:
                history_asu = max_asu
                max_asu = now_ASUs[i]
                min = 10000
            if (min > now_MSSEs[i]) and (history_asu < now_ASUs[i]):
                min = now_MSSEs[i]
            ob[i-1, j] = min
    means = np.mean(ob, axis=1)
    std_devs = np.std(ob, axis=1)
    return means, std_devs

def get_oneMinMSSE(origin_file_name1, origin_file_asu1, index):
    # 绘制最小MSSE origin_file_name1:MSSE
    data = pd.read_csv(origin_file_name1, index_col=0)
    data_asu = pd.read_csv(origin_file_asu1, index_col=0)
    msse_ = data.columns.tolist()  # ["MSSE_141", "MSSE_515", "MSSE_407", "MSSE_221", "MSSE_350"]
    asu_ = data_asu.columns.tolist()  # ["ASU_141", "ASU_515", "ASU_407", "ASU_221", "ASU_350"]
    ob = np.zeros((data[msse_[0]].shape[0], len(msse_)))
    for j in range(len(msse_)):
        min = 10000
        now_ASUs = data_asu[asu_[j]]
        max_asu = now_ASUs[0]
        now_MSSEs = data[msse_[j]]
        history_asu = 0.0
        for i in range(1, len(now_MSSEs)):
            if max_asu < now_ASUs[i]:
                history_asu = max_asu
                max_asu = now_ASUs[i]
                min = 10000
            if (min > now_MSSEs[i]) and (history_asu < now_ASUs[i]):
                min = now_MSSEs[i]
            ob[i-1, j] = min

    return ob[:, index]


def pltOneASU_minMSSE_abstract(asu_file_name, msse_file_name, beilv):
    index = 0
    minMSSE = get_oneMinMSSE(msse_file_name, asu_file_name, index)
    asu_data = pd.read_csv(asu_file_name)
    colone_name = asu_data.columns.tolist()[index + 1]
    asu = asu_data[colone_name]
    asu_reshaped_series = asu[:-1].values.reshape(-1, beilv) #ASU长度为3001
    asu_means = np.mean(asu_reshaped_series, axis=1)
    asu_std_devs = np.std(asu_reshaped_series, axis=1)
    asu_upper_bound = asu_means + asu_std_devs
    asu_lower_bound = asu_means - asu_std_devs
    timesteps = np.arange(asu_reshaped_series.shape[0])
    fig, ax1 = plt.subplots()
    color = "tab:red"
    ax1.plot(timesteps, asu_means, color=color)  # add x-axis label
    ax1.set_xlabel('Model', fontsize=14)  # add y-axis label
    ax1.set_ylabel('ASU', fontsize=14)  # define second y-axis that shares x-axis with current plot
    ax1.fill_between(timesteps, asu_lower_bound, asu_upper_bound, alpha=alpha, color=color)
    ax1.tick_params(axis='y')

    print()
    msse_reshaped_series = minMSSE[:-1].reshape(-1, beilv)
    msse_means = np.mean(msse_reshaped_series, axis=1)
    msse_std_devs = np.std(msse_reshaped_series, axis=1)
    upper_bound = msse_means + msse_std_devs
    lower_bound = msse_means - msse_std_devs
    ax2 = ax1.twinx()
    color = 'tab:blue'
    ax2.plot(timesteps, msse_means, color=color)  # add x-axis label
    ax2.set_ylabel('MSSE', fontsize=14)  # define second y-axis that shares x-axis with current plot
    ax2.fill_between(timesteps, lower_bound, upper_bound, alpha=alpha, color=color)
    ax2.tick_params(axis='y')
    fig.tight_layout()  # otherwise the right y-label is slightly clipped
    fig.set_size_inches(10, 6, forward=True)

    plt.savefig("best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/img/oneline_asu_minmsse.png")

    plt.show()

def plt_minMSSE_ASU_abstract(beilv, file_name,saved_path, task_name):
    alpha = 0.3
    #plt.figure(figsize=(8, 5))
    data_plt = pd.read_csv(file_name)
    colmn_name = data_plt.columns.tolist()
    asu_means = data_plt[colmn_name[1]]
    #asu_std_devs = data_plt[colmn_name[2]]
    asu_means_reshape = asu_means.values.reshape(-1, beilv)
    asu_mean_reshape_mean = np.mean(asu_means_reshape, axis=1)
    asu_mean_reshape_std = np.std(asu_means_reshape, axis=1)

    msse_means = data_plt[colmn_name[3]]
    msse_means_reshape = msse_means.values.reshape(-1, beilv)
    msse_mean_reshape_mean = np.mean(msse_means_reshape, axis=1)
    msse_mean_reshape_std = np.std(msse_means_reshape, axis=1)

    #msse_std_devs = data_plt[colmn_name[4]]
#ASU
    upper_bound = asu_mean_reshape_mean + asu_mean_reshape_std
    lower_bound = asu_mean_reshape_mean - asu_mean_reshape_std
    timesteps = np.arange(asu_means_reshape.shape[0])
    fig, ax1 = plt.subplots()
    color = "tab:red"
    ax1.plot(timesteps, asu_mean_reshape_mean, color=color)  # add x-axis label
    ax1.set_xlabel('Model', fontdict=fontStyle)  # add y-axis label
    ax1.set_ylabel('ASU', fontdict=fontStyle)  # define second y-axis that shares x-axis with current plot
    ax1.fill_between(timesteps, lower_bound, upper_bound, alpha=alpha, color=color)
    ax1.tick_params(axis='y', labelcolor=color)
    # ax1.legend(loc='lower right')
#MSSE
    upper_bound = msse_mean_reshape_mean + msse_mean_reshape_std
    lower_bound = msse_mean_reshape_mean - msse_mean_reshape_std
    ax2 = ax1.twinx()
    color = 'tab:blue'
    ax2.plot(timesteps, msse_mean_reshape_mean, color=color)  # add x-axis label
    ax2.set_ylabel('MSSE', fontdict=fontStyle)  # define second y-axis that shares x-axis with current plot
    ax2.fill_between(timesteps, lower_bound, upper_bound, alpha=alpha, color=color)
    ax2.tick_params(axis='y', labelcolor=color)
    fig.tight_layout()  # otherwise the right y-label is slightly clipped
    fig.set_size_inches(10, 6, forward=True)
    #plt.title(Task_name, fontdict=fontStyle)

    plt.savefig(saved_path)

    plt.show()

def plt_prote_abstract(asu_file_name, msse_file_name, index):
    asu = pd.read_csv(asu_file_name)
    msse = pd.read_csv(msse_file_name)
    asu_colums_name = asu.columns.tolist()
    msse_colums_name = msse.columns.tolist()
    asu_one = asu[asu_colums_name[index]]
    asu_one_4 = []
    for i in asu_one:
        b = int(3 - np.floor(np.log10(i)))  # 原excel公式为=FIXED(B2,3-INT(LOG(B2)),1), 此处表示括号内的东西
        c = np.round(i, b)  # round是四舍六入五进偶
        asu_one_4.append(c)
    msse_one = msse[msse_colums_name[index]]
    asu_name = asu_colums_name[index]
    msse_name = msse_colums_name[index]
    data = {asu_name: asu_one_4,
            msse_name: msse_one
            }
    df = pd.DataFrame(data)
    asumsse_group = df.groupby(asu_name)
    asu_group_list = []
    minmsse_group_list = []
    for i in asumsse_group:
        for j in range(0, 1, len(i)):
            asu_group_list.append(i[j])
            data_j_DataFrame = i[j + 1]
            msse_nam = data_j_DataFrame.columns.tolist()[-1]
            data_j_DataFram_MSSE = data_j_DataFrame[msse_nam].values
            minmsse = np.min(data_j_DataFram_MSSE)
            minmsse_group_list.append(minmsse)
    plt.plot(asu_group_list, minmsse_group_list)
    plt.show()
    # print(a)

if __name__ == '__main__':
    activate_fun_1 = "Sigmoid"
    activate_fun_2 = "Softplus"
    beilv = 25
    alpha = 0.3
    #绘制一条ASU与 min MSSE 的交叉线
    #Task_name = "Task7"
    #base_file_ASU = "best_orders/baseline_/"+Task_name+"/ASU.csv"
    #base_file_MSSE = "best_orders/baseline_/"+Task_name+"/MSSE.csv"

   # saved_path = "best_orders/baseline_/"+Task_name+"/img/minMSSEASU_5_Mean_Mean.png"

    #pltOneASU_minMSSE_abstract(base_file_ASU, base_file_MSSE, beilv)
    #mean_asumsse_path = "best_orders/baseline_/" + Task_name + "/ASU_MSSE.csv"
    #getASU_MSSE(base_file_ASU, base_file_MSSE, mean_asumsse_path)
    #plt_minMSSE_ASU_abstract(beilv, mean_asumsse_path, saved_path,Task_name)
    #绘制帕累托前沿面
    #plt_prote_abstract("best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/ASU.csv", "best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/MSSE.csv", 1)



    #pltMinMSSETask7_sigmoid_softplus(beilv, alpha, "best_orders/baseline_/Task7") #绘制Task7的最小MSSE值
    #pltMinMSSETask8_sigmoid_softplus(beilv, alpha, "best_orders/baseline_/Task8") #绘制Task8的最小MSSE值

    # # #绘制Task8MSSEBar-Sigmoid
    # pltMSSEBar_Abstract("best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/MSSE.csv", "best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/img/MSSEBarGroup_sigmoid.png", "Task8", activate_fun_1, beilv, alpha)
    # # # # #绘制Task8的MSSEBar-Softplus
    # pltMSSEBar_Abstract("best_orders/Task8/Trans_softplus_MSSE_ASU_Reward/MSSE.csv", "best_orders/Task8/Trans_softplus_MSSE_ASU_Reward/img/MSSEBarGroup_softplus.png", "Task8", activate_fun_2, beilv, alpha)
    # # # # #绘制Task7MSSEBar-Sigmoid
    # # # pltMSSEBar_Abstract("best_orders/Task7/Trans_sigmoid_MSSE_ASU_Reward/MSSE.csv", "best_orders/Task7/Trans_sigmoid_MSSE_ASU_Reward/img/MSSEBarGroup_sigmoid.png", "Task7", activate_fun_1, beilv, alpha)
    # # #绘制Task8的MSSEBar-Softplus
    # # # pltMSSEBar_Abstract("best_orders/Task7/Trans_softplus_MSSE_ASU_Reward/MSSE.csv", "best_orders/Task7/Trans_softplus_MSSE_ASU_Reward/img/MSSEBarGroup_softplus.png", "Task7", activate_fun_2, beilv, alpha)
    # # # #绘制ASUBar Task8
    # pltASUBar_Abstract("best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/ASU.csv", "best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/img/ASUBarGroup_sigmoid.png", "Task8", activate_fun_1, beilv, alpha)
    # pltASUBar_Abstract("best_orders/Task8/Trans_softplus_MSSE_ASU_Reward/ASU.csv", "best_orders/Task8/Trans_softplus_MSSE_ASU_Reward/img/ASUBarGroup_softplus.png", "Task8", activate_fun_2, beilv, alpha)
    # # # #绘制ASUBar Task7
    # # # # pltASUBar_Abstract("best_orders/Task7/Trans_sigmoid_MSSE_ASU_Reward/ASU.csv", "best_orders/Task7/Trans_sigmoid_MSSE_ASU_Reward/img/ASUBarGroup_sigmoid.png", "Task7", activate_fun_1, beilv, alpha)
    # # # # pltASUBar_Abstract("best_orders/Task7/Trans_softplus_MSSE_ASU_Reward/ASU.csv", "best_orders/Task7/Trans_softplus_MSSE_ASU_Reward/img/ASUBarGroup_softplus.png", "Task7", activate_fun_2, beilv, alpha)
    # # # # # #绘制RewardBar Task7
    # # # pltRewardBar_Abstract("best_orders/Task7/Trans_sigmoid_MSSE_ASU_Reward/Reward.csv", "best_orders/Task7/Trans_sigmoid_MSSE_ASU_Reward/img/RewardBarGroup_sigmoid.png", "Task7", activate_fun_1, beilv, alpha)
    # # # pltRewardBar_Abstract("best_orders/Task7/Trans_softplus_MSSE_ASU_Reward/Reward.csv", "best_orders/Task7/Trans_softplus_MSSE_ASU_Reward/img/RewardBarGroup_softplus.png", "Task7", activate_fun_2, beilv, alpha)
    # # # # #绘制RewardBar Task8
    # pltRewardBar_Abstract("best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/Rewards.csv", "best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/img/RewardBarGroup_sigmoid.png", "Task8", activate_fun_1, beilv, alpha)
    # pltRewardBar_Abstract("best_orders/Task8/Trans_softplus_MSSE_ASU_Reward/Rewards.csv", "best_orders/Task8/Trans_softplus_MSSE_ASU_Reward/img/RewardBarGroup_softplus.png", "Task8", activate_fun_2, beilv, alpha)
    # # # # #绘制TopsisBar Task7
    # # # # pltTopsisBar_Abstract("best_orders/Task7/Trans_sigmoid_MSSE_ASU_Reward/Topsis.csv", "best_orders/Task7/Trans_sigmoid_MSSE_ASU_Reward/img/TopsisBarGroup_sigmoid.png", "Task7", activate_fun_1, beilv, alpha)
    # # # # pltTopsisBar_Abstract("best_orders/Task7/Trans_softplus_MSSE_ASU_Reward/Topsis.csv", "best_orders/Task7/Trans_softplus_MSSE_ASU_Reward/img/TopsisBarGroup_softplus.png", "Task7", activate_fun_2, beilv, alpha)
    # # # # #绘制TopsisBar Task8
    # pltTopsisBar_Abstract("best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/Topsis.csv", "best_orders/Task8/Trans_sigmoid_MSSE_ASU_Reward/img/TopsisBarGroup_sigmoid.png", "Task8", activate_fun_1, beilv, alpha)
    # pltTopsisBar_Abstract("best_orders/Task8/Trans_softplus_MSSE_ASU_Reward/Topsis.csv", "best_orders/Task8/Trans_softplus_MSSE_ASU_Reward/img/TopsisBarGroup_softplus.png", "Task8", activate_fun_2, beilv, alpha)



